{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4654654sdfdfd\n",
      "普通方法\n",
      "None\n",
      "类方法%s\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "class demo():\n",
    "    def __init__(self,title):\n",
    "        self.title=title\n",
    "    @classmethod\n",
    "    def fujun(cls):\n",
    "        \n",
    "        \n",
    "        print('类方法%s')\n",
    "    def fuwen(self):\n",
    "        print('普通方法')\n",
    "if __name__=='__main__':\n",
    "    res=demo('4654654sdfdfd')\n",
    "    print(res.title)\n",
    "    print(demo(1).fuwen())\n",
    "   \n",
    "    print(demo.fujun())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class '__main__.p'>\n"
     ]
    }
   ],
   "source": [
    "class p:\n",
    "    pass\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "class foo:\n",
    "    x=1\n",
    "    y=''\n",
    "    def test(self):\n",
    "        print('123')\n",
    "v=foo()\n",
    "print(hasattr(v,'k'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "__init__() takes 1 positional argument but 4 were given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-45-11fa52271d45>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      8\u001b[0m             \u001b[0msing\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_instace\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msing\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0msing\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_instance\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msing\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4654\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'jglfjgk'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m45654\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-45-11fa52271d45>\u001b[0m in \u001b[0;36minstance\u001b[1;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[0;32m      6\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0minstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcls\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msing\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"_istance\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m             \u001b[0msing\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_instace\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msing\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0msing\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_instance\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msing\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4654\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'jglfjgk'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m45654\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: __init__() takes 1 positional argument but 4 were given"
     ]
    }
   ],
   "source": [
    "class sing:\n",
    "    def __init__(self):\n",
    "        import time\n",
    "        time.sleep(1)\n",
    "    @classmethod\n",
    "    def instance(cls,*args,**kwargs):\n",
    "        if not hasattr(sing,\"_istance\"):\n",
    "            sing._instace=sing(*args,**kwargs)\n",
    "        return sing._instance\n",
    "    \n",
    "print(sing.instance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<__main__.Singleton object at 0x000002979EA1CD30>\n",
      "None\n",
      "<__main__.Singleton object at 0x000002979EA1CD30>\n",
      "<__main__.Singleton object at 0x000002979EA1CD30>\n",
      "<__main__.Singleton object at 0x000002979EA1CD30>\n",
      "<__main__.Singleton object at 0x000002979EA1CD30>\n",
      "<__main__.Singleton object at 0x000002979EA1CD30>\n",
      "<__main__.Singleton object at 0x000002979EA1CD30>\n",
      "<__main__.Singleton object at 0x000002979EA1CD30>\n",
      "<__main__.Singleton object at 0x000002979EA1CD30>\n",
      "<__main__.Singleton object at 0x000002979EA1CD30>\n",
      "<__main__.Singleton object at 0x000002979EA1CD30>\n"
     ]
    }
   ],
   "source": [
    "class Singleton(object):\n",
    "    def __init__(self):\n",
    "        import time\n",
    "        time.sleep(2)\n",
    "    @classmethod\n",
    "    def instance(cls,*args,**kwargs):\n",
    "        if not hasattr(Singleton,\"_instance\"):\n",
    "            Singleton._instance=Singleton(*args,**kwargs)\n",
    "        return Singleton._instance\n",
    "import threading\n",
    "def task(arg):\n",
    "    obj=Singleton.instance()\n",
    "    print(obj)\n",
    "print(task(12))\n",
    "for i in range(10):\n",
    "    t=threading.Thread(target=task,args=[i,])\n",
    "    t.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<__main__.Singleton object at 0x000002979EA44B00><__main__.Singleton object at 0x000002979EA44B00>\n",
      "\n",
      "<__main__.Singleton object at 0x000002979EA44B00>\n",
      "<__main__.Singleton object at 0x000002979EA44B00>\n",
      "<__main__.Singleton object at 0x000002979EA44B00>\n",
      "<__main__.Singleton object at 0x000002979EA44B00><__main__.Singleton object at 0x000002979EA44B00>\n",
      "\n",
      "<__main__.Singleton object at 0x000002979EA44B00>\n",
      "<__main__.Singleton object at 0x000002979EA44B00><__main__.Singleton object at 0x000002979EA44B00>\n",
      "\n",
      "<__main__.Singleton object at 0x000002979EA44B00>\n"
     ]
    }
   ],
   "source": [
    "import time \n",
    "import threading\n",
    "class Singleton(object):\n",
    "    _instance_lock=threading.Lock()\n",
    "    \n",
    "    def __init__(self):\n",
    "        time.sleep(2)\n",
    "    @classmethod\n",
    "    def instance(cls,*args,**kwargs):\n",
    "        if not hasattr(Singleton,\"_instance\"):\n",
    "            with Singleton._instance_lock:\n",
    "                if not hasattr(Singleton,\"_instance\"):\n",
    "                    Singleton._instance=Singleton(*args,**kwargs)\n",
    "        return Singleton._instance\n",
    "    \n",
    "def task(arg):\n",
    "    obj=Singleton.instance()\n",
    "    print(obj)\n",
    "for i in range(10):\n",
    "    t=threading.Thread(target=task,args=[i,])\n",
    "    t.start()\n",
    "time.sleep(20)\n",
    "obj=Singleton.instance()\n",
    "print(obj)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<__main__.Singleton object at 0x000002979EA44C18>\n",
      "<__main__.Singleton object at 0x000002979EA44C18>\n",
      "<__main__.Singleton object at 0x000002979EA44C18>\n",
      "<__main__.Singleton object at 0x000002979EA44C18>\n",
      "<__main__.Singleton object at 0x000002979EA44C18><__main__.Singleton object at 0x000002979EA44C18>\n",
      "\n",
      "<__main__.Singleton object at 0x000002979EA44C18>\n",
      "<__main__.Singleton object at 0x000002979EA44C18>\n",
      "<__main__.Singleton object at 0x000002979EA44C18>\n",
      "<__main__.Singleton object at 0x000002979EA44C18>\n"
     ]
    }
   ],
   "source": [
    "import threading \n",
    "class Singleton(object):\n",
    "    _instance_lock=threading.Lock()\n",
    "    def __init__(self):\n",
    "        pass\n",
    "    def __new__(cls,*args,**kwargs):\n",
    "        if not hasattr(Singleton,\"_instance\"):\n",
    "            with Singleton._instance_lock:\n",
    "                if not hasattr(Singleton,\"_instance\"):\n",
    "                    Singleton._instance=object.__new__(cls)\n",
    "        return Singleton._instance\n",
    "def task(arg):\n",
    "    obj=Singleton()\n",
    "    print(obj)\n",
    "for i in range(10):\n",
    "    t=threading.Thread(target=task,args=[i,])\n",
    "    t.start()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-9-d788545a43d5>, line 19)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-9-d788545a43d5>\"\u001b[1;36m, line \u001b[1;32m19\u001b[0m\n\u001b[1;33m    if not hasattr(cls,\"_instance\")\u001b[0m\n\u001b[1;37m                                   ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "class Singleton(type):\n",
    "    def __init__(self,*args,**kwargs):\n",
    "        print(self)\n",
    "        super(Singleton,self).__init__(*args,**kwargs)\n",
    "    def __call__(cls,*args,**kwargs):\n",
    "        obj=cls.__new__(cls,*args,**kwargs)\n",
    "        obj.__init__(*args,**kwargs)\n",
    "        return obj\n",
    "class Foo(metaclass=Singleton):\n",
    "    def __init__(self,name):\n",
    "        self.name=name\n",
    "    def __new__(cls,*args,**kwargs):\n",
    "        return object.__new__(cls,*args,**kwargs)\n",
    "obj=Foo('fujun')\n",
    "import threading\n",
    "class Singleton(type):\n",
    "    _instance_lock=threading.Lock()\n",
    "    def __call__(cls,*args,**kwargs):\n",
    "        if not hasattr(cls,\"_instance\"):\n",
    "            with Singleton._instace_lock:\n",
    "                if not hasattr(cls,\"_instance\")\n",
    "                    cls._instance=super(Singleton,cls).__call__(*args,**kwargs)\n",
    "        return cls._instance\n",
    "class Foo(metaclass=Singleton):\n",
    "    def __init__(self,name):\n",
    "        self.name=name\n",
    "obj1=Foo('anme')\n",
    "obj2=Foo('anme')\n",
    "print(obj1,obj2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class '__main__.Singleton'>\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-24-88368da17f1d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     14\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mage\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mage\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m \u001b[0mobj1\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mSingleton\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'fujun'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m45\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     17\u001b[0m \u001b[0mobj2\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mSingleton\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'fuwen'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m123\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-24-88368da17f1d>\u001b[0m in \u001b[0;36minner\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m      5\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mcls\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[0minstance\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m             \u001b[0minstance\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mcls\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcls\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m             \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minstance\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      8\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0minstance\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mcls\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0minner\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 0"
     ]
    }
   ],
   "source": [
    "def wrapper(cls):\n",
    "    print(cls)\n",
    "    instance={}\n",
    "    def inner(*args,**kwargs):\n",
    "        if cls not in instance:\n",
    "            instance[cls]=cls(*args,**kwargs)\n",
    "            \n",
    "        return instance[cls]\n",
    "    return inner\n",
    "@wrapper\n",
    "class Singleton(object):\n",
    "    def __init__(self,name,age):\n",
    "        self.name=name\n",
    "        self.age=age\n",
    "    \n",
    "obj1=Singleton('fujun',45)\n",
    "obj2=Singleton('fuwen',123)\n",
    "\n",
    "print(obj1,obj2)\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']\n",
      "{1, 'ghg', 7897, 'djfgdk'}\n"
     ]
    }
   ],
   "source": [
    "set={1,'ghg',56,'djfgdk',56}\n",
    "print(dir(set))\n",
    "set.add(7897)\n",
    "set.remove(56)\n",
    "print(set)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
